package objectio;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

import org.junit.Test;

public class ObjectStreamDemo {
	@Test
	public void write() {
		try (FileOutputStream fos = new FileOutputStream("a.txt");
			ObjectOutputStream oos = new ObjectOutputStream(fos);){
			// 写不同数据类型
			oos.writeInt(100);
			oos.writeBoolean(true);
			oos.writeDouble(1.1);
			oos.writeUTF("haha");
			oos.flush();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	@Test
	public void read() {
		try (FileInputStream fos = new FileInputStream("a.txt");
			ObjectInputStream oos = new ObjectInputStream(fos);){
			// 读的时候一定要按照写的顺序读
			System.out.println(oos.readInt());
			System.out.println(oos.readBoolean());
			System.out.println(oos.readDouble());
			System.out.println(oos.readUTF());
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
	@Test
	public void writeObj() {
		try (FileOutputStream fos = new FileOutputStream("a.txt");
			ObjectOutputStream oos = new ObjectOutputStream(fos);){
			// 写不同数据类型
			// 写出去的对象的类必须实现Serializable接口
			oos.writeObject(new Person("zs",20,'男',99999));
			oos.flush();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@Test
	public void readObj() {
		try (FileInputStream fos = new FileInputStream("a.txt");
			ObjectInputStream oos = new ObjectInputStream(fos);){
			// 读的时候一定要按照写的顺序读
			System.out.println(oos.readObject());
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	@Test
	public void writeList() {
		try (FileOutputStream fos = new FileOutputStream("a.txt");
			ObjectOutputStream oos = new ObjectOutputStream(fos);){
			// 写不同数据类型
			// 写出去的对象的类必须实现Serializable接口
			List<Person> list = new ArrayList<>();
			list.add(new Person("zs",20,'男',99999));
			list.add(new Person("zs2",20,'男',99999));
			list.add(new Person("zs3",20,'男',99999));
			list.add(new Person("zs4",20,'男',99999));
			list.add(new Person("zs5",20,'男',99999));
			oos.writeObject(list);
			oos.flush();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	@Test
	public void readList() {
		try (FileInputStream fos = new FileInputStream("a.txt");
			ObjectInputStream oos = new ObjectInputStream(fos);){
			// 读的时候一定要按照写的顺序读
			System.out.println(oos.readObject());
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
}
